<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head>
	<META http-equiv="Content-Type" content="text/html; charset=utf-8">
	<link rel="icon" type="image/vnd.microsoft.icon" href="http://www.gstatic.com/codesite/ph/images/phosting.ico">
	<script type="text/javascript" src="js/jquery.js"></script>
	<script type="text/javascript" src="js/utils.js"></script>
	<script type="text/javascript" src="js/dbg.js"></script>

<script type="text/javascript">

var infoBox = null;
var curFile = '';

$(function(){

	infoBox = $("#infoBox");
	
	$("#btnClear").bind("click", function(){
		ajaxoptions.url = 'ajax.php?page=act&cmd=clear';
		ajaxoptions.success = function(result, status)
		{
			infoBox.text("Clear: "+ result + "\n" + infoBox.text());
		}
		$.ajax(ajaxoptions);	
	});
	
	$("#btnRun").bind("click", function(){
		ajaxoptions.url = 'ajax.php?page=act&cmd=dir&dir=data';
		ajaxoptions.success = function(files, status)
		{
			for(var i = 0; i < files.length; i++)
			{
				curFile = files[i];
				// console.log(curFile);
				
				$.ajax({
					url     : 'ajax.php?page=act&cmd=run&file=data/' + curFile,
					type    : 'POST',
					dataType: 'json',
					async	: true,
					success : function(result, status)
					{
						var dt = new Date();
						log("Executing status is "+ result);
					},
					error   : function(xhr, status, error) {
						console.log(xhr);
					}
				});
				
			}	
			
			// console.log(files);
		}
		$.ajax(ajaxoptions);	
	});
	
	
});


function log(msg)
{
	var dt = new Date();
	// var s = dt.toTimeString();
	var s = dt.toRusTime();
	infoBox.text(s + "\t" + msg + "\n" + infoBox.text());
}

var ajaxoptions = {
	type    : 'POST',
	url     : 'ajax.php?page',
	dataType: 'json',
	async	: true,
	error   : function(xhr, status, error) {
		console.log("error: " + var_dump(xhr));
	}
}

</script>
</head>
<body style="margin: 32px;">
	<button id="btnClear">Очистить историю сообщений</button>
	<br/>
	<br/>
	<button id="btnRun">Обработать сообщения</button>
	<br/>
	<br/>
	<pre id="infoBox">
		
	</pre>
</body>
</html>